#include <nlohmann/json.hpp>
#include "../include/CandidateResult.h"
#include "../include/KeyRecommander.h"
#include "../include/Dictionary.h"
#include "sstream"
#include <iostream>
using std::cout;
using std::endl;

using json = nlohmann::json;
using std::stringstream;
KeyRecommander::KeyRecommander(const string& word, TcpConnectionPtr& conn)
:_sought(word)
,_conn(conn)
{

}
void from_json(const json& j, string& val)
{
    
}

void to_json(json& j, const string& val)
{
}
string KeyRecommander::doQuery()
{
    Dictionary* dict = Dictionary::getInstance();
    _prique = dict->doQuery(_sought);
    
    //至多推荐10个关键字
    vector<string> result;
    int cnt = 0;
    for(auto it = _prique.begin(); it != _prique.end() && cnt < 10; ++it,++cnt)
    {
        result.push_back(it->_word);
    }
    //vector<string>转成js对象
    //js对象转成js字符串
    //返回js字符串
    json js = result;
    string res = js.dump();
    return res;
}
